package com.dao;

import com.baidu.StudentTest;
import com.jdbc.JdbcTest;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Student extends JdbcTest{


    Connection connection;
    PreparedStatement preparedStatement;
    ResultSet resultSet;

    //查询
    public List check() throws SQLException {
        connection = JdbcTest.getConnection();
        preparedStatement = connection.prepareStatement("select * from student");
        resultSet = preparedStatement.executeQuery();
        List<StudentTest> userArrayList = new ArrayList();
        while (resultSet.next()) {
           userArrayList.add(new StudentTest(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3),resultSet.getString(4), resultSet.getInt(5),resultSet.getString(6)));

        }
        return userArrayList;
    }

//    增
    public void increase(Integer sid, String studentno, String sname, String gender, Integer age, String address) throws SQLException, IOException, ClassNotFoundException {
        System.out.println(connection);
        System.out.println(preparedStatement);
        connection = JdbcTest.getConnection();
        preparedStatement= connection.prepareStatement("insert into student values (?,?,?,?,?,?)");
        preparedStatement.setInt(1,sid);
        preparedStatement.setString(2,studentno);
        preparedStatement.setString(3,sname);
        preparedStatement.setString(4,gender);
        preparedStatement.setInt(5,age);
        preparedStatement.setString(6,address);
        preparedStatement.executeUpdate();

    }

    //修改
    public void updataStudent(Integer sid, String studentno, String sname, String gender, Integer age, String address) throws SQLException {
        System.out.println(connection);
        System.out.println(preparedStatement);
        connection = JdbcTest.getConnection();
        preparedStatement = connection.prepareStatement("update student set studentno=?,sname=?,gender=?,age=?,address=? where sid=?");
        preparedStatement.setString(1,studentno);
        preparedStatement.setString(2,sname);
        preparedStatement.setString(3,gender);
        preparedStatement.setInt(4,age);
        preparedStatement.setString(5,address);
        preparedStatement.setInt(6,sid);
        preparedStatement.executeUpdate();
        }

        //删除
    public void deleteData(Integer sid) throws SQLException {
        connection = JdbcTest.getConnection();
        preparedStatement = connection.prepareStatement("delete from student where sid=?");
        preparedStatement.setInt(1,sid);
        preparedStatement.executeUpdate();

    }
}
